package com.gzhryc.shared_device.oem.h5.mng.controllers.device;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.device.dao.db.DeviceTypeFunc;
import com.gzhryc.shared_device.common.device.services.DeviceTypeFuncService;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.code.device.EDeviceFunCode;

public class DeviceTypeFuncController {

    @Route(value = "/device/type/func/list.data")
    public ListResult<DeviceTypeFunc> listData(HttpServletRequest request) {
        ListResult<DeviceTypeFunc> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        String typeId = WebUtils.getString(request,"typeId");
        result.getBody().setData(DeviceTypeFuncService.self().findByTypeId(typeId));
        if (result.getBody().getData() != null && result.getBody().getData().size() > 0) {
            for(DeviceTypeFunc deviceTypeFunc : result.getBody().getData()){
                deviceTypeFunc.setBusinessTypeName(EBusinessType.note(deviceTypeFunc.getBusinessType()));
            }
            result.setCode(DtoConstants.SUCCESS,null);
        }
        return result;
    }

    @Route(value = { "/device/type/func/add.html" })
    public String add(HttpServletRequest request) {
        request.setAttribute("deviceFuncCodeList", EDeviceFunCode.values());
        request.setAttribute("businessTypeList", EBusinessType.values());
        return "/device/type/func/edit.html";
    }

    @Route(value = { "/device/type/func/add.do" })
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"新增失败，请稍后再试");
        DeviceTypeFunc deviceTypeFunc = WebUtils.getParameter(request,DeviceTypeFunc.class);
        try {
            EDeviceFunCode funcCode = EDeviceFunCode.valueOf(deviceTypeFunc.getCode());
            if (funcCode == null) {
                result.setMsg("未找到设备功能定义");
                return result;
            }
            deviceTypeFunc.setCodeName(funcCode.note());
            if(DeviceTypeFuncService.self().add(deviceTypeFunc)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/device/type/func/update.html" })
    public String update(HttpServletRequest request) {
        String typeId = WebUtils.getString(request,"typeId");
        String code = WebUtils.getString(request,"code");
        DeviceTypeFunc deviceTypeFunc = DeviceTypeFuncService.self().getByKeys(typeId,code);
        WebUtils.setAttribute(request,deviceTypeFunc);
        request.setAttribute("businessTypeList", EBusinessType.values());
        return "/device/type/func/edit.html";
    }

    @Route(value = { "/device/type/func/update.do" })
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"修改失败，请稍后再试");
        DeviceTypeFunc deviceTypeFunc = WebUtils.getParameter(request,DeviceTypeFunc.class);
        try {
            if(DeviceTypeFuncService.self().updateNotNull(deviceTypeFunc)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/device/type/func/cost.html" })
    public String cost(HttpServletRequest request, HttpServletResponse response) {
        String typeId = WebUtils.getString(request,"typeId");
        String code = WebUtils.getString(request,"code");
        if(EDeviceFunCode.CDCZ.index().equals(code)){
            WebUtils.sendRedirect(request,response,"/cdcz/device/type/cost.html?typeId="+typeId);
        }
        return null;
    }

    @Route(value = { "/device/type/func/del.do" })
    public AjaxResult doDelete(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除失败，请稍后再试");
        String typeId = WebUtils.getString(request,"typeId");
        String code = WebUtils.getString(request,"code");
        try {
            if(DeviceTypeFuncService.self().deleteByKeys(typeId,code)) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
